﻿// From: http://www.codeproject.com/KB/WPF/PieChartDataBinding.aspx
// With small changes from Patrick Boos

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel;
using System.Collections.Specialized;
using Microsoft.Surface.Presentation.Controls;

namespace ScrumTable.UI.View.WinTouch.UserControls.PieChart.PieChart
{
    /// <summary>
    /// A pie chart legend
    /// </summary>
    public partial class Legend : UserControl
    {
        private CollectionView _myCollectionView;
        #region dependency properties

        /// <summary>
        /// The property of the bound object that will be plotted
        /// </summary>
        public String PlottedProperty
        {
            get { return PieChartLayout.GetPlottedProperty(this); }
            set { PieChartLayout.SetPlottedProperty(this, value); }
        }

        /// <summary>
        /// The property of the bound object that will be used as color
        /// </summary>
        public String ColorProperty
        {
            get { return PieChartLayout.GetColorProperty(this); }
            set { PieChartLayout.SetColorProperty(this, value); }
        }

        /// <summary>
        /// A class which selects a color based on the item being rendered.
        /// </summary>
        public IColorSelector ColorSelector
        {
            get { return PieChartLayout.GetColorSelector(this); }
            set { PieChartLayout.SetColorSelector(this, value); }
        }

        #endregion

        public Legend()
        {
            // register any dependency property change handlers
            /*
            DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(PieChartLayout.PlottedPropertyProperty, typeof(PiePlotter));
            dpd.AddValueChanged(this, PlottedPropertyChanged);
            */
            DataContextChanged += DataContextChangedHandler;

            InitializeComponent();
        }

        ~Legend()
        {
            Dispose(false);
        }


        /// <summary>
        /// Implement dispose interface in order to support runtime support.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);

            // Use SupressFinalize in case a subclass
            // of this type implements a finalizer.
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Disposes the managed and unmanaged resources.
        /// </summary>
        /// <param name="disposing">True to dispose the managed resources, otherwise false.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (_myCollectionView != null)
            {
                _myCollectionView.CurrentChanged -= new EventHandler(CollectionViewCurrentChanged);

                foreach (object item in _myCollectionView)
                {
                    if (item is INotifyPropertyChanged)
                    {
                        INotifyPropertyChanged observable = (INotifyPropertyChanged) item;
                        observable.PropertyChanged -= ItemPropertyChanged;
                    }
                }
            }
        }


        #region property change handlers

        /// <summary>
        /// Handle changes in the datacontext. When a change occurs handlers are registered for events which
        /// occur when the collection changes or any items within teh collection change.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataContextChangedHandler(object sender, DependencyPropertyChangedEventArgs e)
        {
            // handle the events that occur when the bound collection changes
            if (DataContext is INotifyCollectionChanged)
            {
               // INotifyCollectionChanged observable = (INotifyCollectionChanged)DataContext;
                //observable.CollectionChanged += BoundCollectionChanged;
            }

            ObserveBoundCollectionChanges();
        }

        #endregion

        #region event handlers

        /// <summary>
        /// Handles events which are raised when the bound collection changes (i.e. items added/removed)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BoundCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            RefreshView();
            ObserveBoundCollectionChanges();
        }

        /// <summary>
        /// Handles changes to the PlottedProperty property.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PlottedPropertyChanged(object sender, EventArgs e)
        {
            RefreshView();
        }

        /// <summary>
        /// Iterates over the items inthe bound collection, adding handlers for PropertyChanged events
        /// </summary>
        private void ObserveBoundCollectionChanges()
        {
            _myCollectionView = (CollectionView)CollectionViewSource.GetDefaultView(DataContext);
            
            if (_myCollectionView == null)
            {
                return;
            }
            _myCollectionView.CurrentChanged += new EventHandler(CollectionViewCurrentChanged);
            
            foreach (object item in _myCollectionView)
            {
                if (item is INotifyPropertyChanged)
                {
                    INotifyPropertyChanged observable = (INotifyPropertyChanged)item;
                    observable.PropertyChanged += ItemPropertyChanged;
                }
            }
        }


        /// <summary>
        /// Handles events which occur when the properties of bound items change.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            // if the property which this pie chart represents has changed, re-construct the pie
            if (e.PropertyName.Equals(PlottedProperty))
            {
                RefreshView();
            }
        }

        #endregion

        /// <summary>
        /// Refreshes the view, re-computing any value which is derived from the data bindings
        /// </summary>
        private void RefreshView()
        {
            // when the PlottedProperty changes we need to recompute our bindings. However,
            // the legend is bound to the collection items, the properties of which have not changes.
            // Therefore, we use a bit of an ugly hack to fool the legend into thinking the datacontext
            // has changed which causes it to replot itself.
            object context = legend.DataContext;
            if (context != null)
            {
                legend.DataContext = null;
                legend.DataContext = context;
            }
        }

        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listbox = sender as SurfaceListBox;
            if(listbox==null) return;

            listbox.ScrollIntoView(listbox.SelectedItem);
        }


        /// <summary>
        /// Handles the event which occurs when the selected item has changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CollectionViewCurrentChanged(object sender, EventArgs e)
        {
            CollectionView collectionView = (CollectionView)sender;

            if (collectionView != null && collectionView.CurrentPosition >= 0 )
            {
                this.legend.SelectedIndex = collectionView.CurrentPosition;
            }
        }
    }
}